import { app, shell, BrowserWindow ,ipcMain} from 'electron'
import path, { join } from 'path'

const reload = require('electron-reload')
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
import icon from '../../resources/icon.png?asset'
import axios from 'axios'
import { message } from 'antd';
const {exec} = require('child_process')
const  fs = require('fs')
		


		
const os = require('os');

// 获取系统启动时间


// 将秒数转换为更友好的格式（天、小时、分钟、秒）


reload(__dirname)
  function createWindow(): void {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 500,
    show: false,
    autoHideMenuBar: true,
    ...(process.platform === 'linux' ? { icon } : {}),
    webPreferences: {
      preload: join(__dirname, '../preload/index.js'),
      sandbox: false
    }
  })



//定时获取开机时间
 setInterval( async()=>{
    const uptime = os.uptime();
  const days = Math.floor(uptime / (60 * 60 * 24));
const hours = Math.floor((uptime % (60 * 60 * 24)) / (60 * 60));
const minutes = Math.floor((uptime % (60 * 60)) / 60);
const seconds = Math.floor(uptime % 60);
const uptimedata = `${days}天 ${hours}小时 ${minutes}分钟`
console.log(uptimedata)
const textdata= path.join('c:',"xiaolu","user.txt")
fs.readFile(textdata,"utf8",(err,stdout)=>{
  if(!err){
    console.log("123")
   
  axios
      .get('http://node.dayuan0307.top:300/gf')
      .then(async (resa) => {
     const gfa=resa.data
     console.log(resa.data)
        await axios
          .get('http://127.0.0.1:31401')
          .then(async (res) => {
            const a = res.data.core_latest_ledger
            const b = resa.data
  
            const a1 = a === 1
            const c = a - b
          
            const c1 = c < 1200 || c < 0
            const c2 = c > 5000
            if (a1) {
              
              await axios
                .post('http://node.dayuan0307.top:300/zhihui/ed', {
                  name: stdout,
                 
                  startup:uptimedata,
                  status: '新作',
                })
                .then((res) => {
                 
                })
            }
            if (c1) {
            
               axios
                .post('http://node.dayuan0307.top:300/zhihui/ed', {
                  
                name: stdout,
                 
                startup:uptimedata,
                  status:'正常'
                })
                .then((res) => {
              
                })
            }
            if (c2) {
             
              await axios
                .post('http://node.dayuan0307.top:300/zhihui/ed', {
                  name: stdout,
                 
                  startup:uptimedata,
                  status: '延迟'
                })
                .then((res) => {
             
                })
            }
          })
          .catch(async (err) => {
          
            await axios
              .post('http://node.dayuan0307.top:300/zhihui/ed', {
                name: stdout,
                 
                startup:uptimedata,
                status: '不正常',
                
              })
              .then((res) => {
                console.log("2222")
               
              })
          })
      })
      .catch((err) => {
       
      })
  }

}
 
)
},20000)
 

  mainWindow.webContents.on('before-input-event',(event,input)=>{
    if(input.key === 'F9'){
       mainWindow.webContents.openDevTools()
       reload(__dirname)
     
    }
    
  })

  mainWindow.on('ready-to-show', () => {
    mainWindow.show()
  
    
        
 
   


  })

  mainWindow.webContents.setWindowOpenHandler((details) => {
    shell.openExternal(details.url)
    return { action: 'deny' }
  })


  // HMR for renderer base on electron-vite cli.
  // Load the remote URL for development or the local html file for production.
  if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
    mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'])
  } else {
    mainWindow.loadFile(join(__dirname, '../renderer/index.html'))
  }
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
  // Set app user model id for windows
  electronApp.setAppUserModelId('com.electron')

  // Default open or close DevTools by F12 in development
  // and ignore CommandOrControl + R in production.
  // see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils
  app.on('browser-window-created', (_, window) => {
    optimizer.watchWindowShortcuts(window)
  })

  createWindow()
  




 

  app.on('activate', function () {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})



// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

// In this file you can include the rest of your app"s specific main process
// code. You can also put them in separate files and require them here.
